<?php
/**
 * Created by PhpStorm.
 * User: lelouchms
 * Date: 2019/3/29
 * Time: 15:17
 */

namespace app\common\model;


use app\common\exception\CustomerException;
use think\facade\Cache;
use think\Model;

class BaseModel extends Model
{

    const STATUS_DISABLED = 2;
    const STATUS_ENABLED = 1;

    const STATUS_DELETED = 1;
    const STATUS_UN_DELETED = 2;

    public function getStatusList()
    {
        return [
            self::STATUS_ENABLED => "启用",
            self::STATUS_DISABLED => "禁用"
        ];
    }

    public function getDeleteList()
    {
        return [
            self::STATUS_DELETED => "已删除",
            self::STATUS_UN_DELETED => "未删除"
        ];
    }

    public function getTypeList()
    {
        return [
            1 => "支付宝",
            2 => "微信"
        ];
    }

    public function getIsCloseList()
    {
        return [
            1 => "已取消",
            2 => "未取消"
        ];
    }

    public function findById($id)
    {
        $cacheKey = $this->name . ":" . $id;
        $cacheData = Cache::get($cacheKey);
        if (!empty($cacheData)) return $cacheData;
        $findData = $this->where("id", $id)->find();
        if (empty($findData)) throw new CustomerException("未找到相关的数据信息");
        Cache::set($cacheKey, $findData, 60);
        return $findData;
    }

    public function getName()
    {
        return $this->name;
    }

    public function getDataByField($field, $value)
    {
        $cacheKey = $this->name . ":" . $field . ":" . $value;
        $cacheData = Cache::get($cacheKey);
        if (!empty($cacheData)) return $cacheData;
        $findData = $this->where($field, $value)->find();
        if (empty($findData)) return null;
        Cache::set($cacheKey, $findData);
        return $findData;
    }
}